home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / tracker-4.13.lha / tracker / resample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-15  |  12.1 KB  |  439 lines

  1. /* resample.c 
  2.     vi:ts=3 sw=3:
  3.  */
  4.  
  5. /* $Id: resample.c,v 4.12 1995/02/14 04:02:28 espie Exp $
  6.  * $Log: resample.c,v $
  7.  * Revision 4.12  1995/02/14  04:02:28  espie
  8.  * Nothing.
  9.  *
  10.  * Revision 4.12  1995/02/14  04:02:28  espie
  11.  * Nothing.
  12.  *
  13.  * Revision 4.11  1995/02/06  14:50:47  espie
  14.  * Changed sample_info.
  15.  *
  16.  * Revision 4.11  1995/02/06  14:50:47  espie
  17.  * Changed sample_info.
  18.  *
  19.  * Revision 4.10  1995/02/01  17:14:54  espie
  20.  * Scaled volume.
  21.  *
  22.  * Revision 4.10  1995/02/01  17:14:54  espie
  23.  * Scaled volume.
  24.  *
  25.  * Revision 4.9  1995/02/01  16:39:04  espie
  26.  * Implemented Left/Right channels, 23 bits output.
  27.  *
  28.  * Revision 4.8  1995/02/01  16:30:36  espie
  29.  * *** empty log message ***
  30.  *
  31.  * Revision 4.7  1995/02/01  15:31:25  espie
  32.  * *** empty log message ***
  33.  *
  34.  * Revision 4.6  1995/01/28  09:23:59  espie
  35.  * *** empty log message ***
  36.  *
  37.  * Revision 4.5  1994/11/15  16:11:01  espie
  38.  * *** empty log message ***
  39.  *
  40.  * Revision 4.5  1994/11/15  16:11:01  espie
  41.  * *** empty log message ***
  42.  *
  43.  * Revision 4.4  1994/08/23  18:19:46  espie
  44.  * New sampling function.
  45.  * Finally corrected irksome probleme with MAX_PITCH.
  46.  * First changes to augment the number of channels.
  47.  * Added one level of abstraction. Look through player.c for the
  48.  * old high-level functions.
  49.  * Optimized output and fixed up some details.
  50.  * Unrolled code for oversample = 1 to be more efficient at
  51.  * higher frequency (since a high frequency is better than
  52.  * a higher oversample).
  53.  *
  54.  * Revision 2.14  1992/11/06  19:31:53  espie
  55.  * Fixed missing parameter type.
  56.  * fix_xxx for better speed.
  57.  * set_volume.
  58.  * Added possibility to get back to MONO for the sgi.
  59.  * Added stereo capabilities to the indigo version.
  60.  * Minor bug: a SAMPLE_FAULT is a minor error,
  61.  * we should first check that there was no other
  62.  * error before setting it.
  63.  * New resample function coming from the player.
  64.  * Added more notes.
  65.  *
  66.  * Revision 2.1  1991/11/17  23:07:58  espie
  67.  * Just computes some frequency-related parameters.
  68.  *
  69.  *
  70.  */
  71.  
  72. #include <math.h>
  73.  
  74. #include "defs.h"
  75. #include "song.h"
  76. #include "channel.h"
  77. #include "tags.h"
  78. #include "extern.h"
  79.      
  80. ID("$Id: resample.c,v 4.12 1995/02/14 04:02:28 espie Exp $")
  81.  
  82. /* DO_NOTHING is also used for the automaton */
  83. #define DO_NOTHING 0
  84. #define PLAY 1
  85. #define REPLAY 2
  86.  
  87. #define MAX_CHANNELS 8
  88.  
  89. LOCAL struct audio_channel
  90.    {
  91.    struct sample_info *samp;
  92.    int mode;
  93.    unsigned long pointer;
  94.    unsigned long step;
  95.    int volume;
  96.     int scaled_volume;
  97.    int pitch;
  98.     int side;
  99.    } chan[MAX_CHANNELS];
  100.  
  101. /* left/right */
  102. #define NUMBER_SIDES 2
  103. LOCAL int total[NUMBER_SIDES];
  104.  
  105. LOCAL int allocated = 0;
  106.  
  107. struct audio_channel *new_channel_tag_list(prop)
  108. struct tag *prop;
  109.    {
  110.    struct audio_channel *new;
  111.  
  112.    new = &chan[allocated++];
  113.    new->mode = DO_NOTHING;
  114.    new->pointer = 0;
  115.    new->step = 0;
  116.    new->pitch = 0;
  117.    new->volume = 0;
  118.     new->scaled_volume = 0;
  119.    new->samp = empty_sample();
  120.  
  121.     while (prop = get_tag(prop))
  122.         {
  123.         switch(prop->type)
  124.             {
  125.         case AUDIO_SIDE:
  126.             new->side = prop->data.scalar;
  127.             break;
  128.         default:
  129.             break;
  130.             }
  131.         prop++;
  132.         }
  133.     total[new->side]++;
  134.    return new;
  135.    }
  136.  
  137. void release_audio_channels()
  138.    {
  139.    allocated = 0;
  140.    }
  141.  
  142. /* Have to get some leeway for vibrato (since we don't bound pitch with
  143.  * vibrato). This is conservative.
  144.  */
  145. #define VIB_MAXDEPTH 150
  146.  
  147.  
  148. #define C fix_to_int(ch->pointer)
  149.  
  150. LOCAL unsigned long step_table[REAL_MAX_PITCH + VIB_MAXDEPTH];  
  151.                   /* holds the increment for finding the next sampled
  152.                    * byte at a given pitch (see resample() ).
  153.                    */
  154.  
  155. /* creates a table for converting ``amiga'' pitch
  156.  * to a step rate at a given resampling frequency.
  157.  * For accuracy, we don't use floating point, but
  158.  * instead fixed point ( << ACCURACY).
  159.  * IMPORTANT NOTES:
  160.  * - we need to make it fit within 32 bits (long), which
  161.  * must be enough for ACCURACY + log2(max sample length)
  162.  * - for the linear resampling to work correctly, we need 
  163.  * sample size (8 bit) + volume size (6 bit) + ACCURACY to fit within a
  164.  * long. Appropriate steps will have to be taken when we switch to 16 bit
  165.  * samples...
  166.  * - never forget that samples are SIGNED numbers. If you have unsigned 
  167.  * samples, you have to convert them SOMEWHERE.
  168.  */
  169.  
  170. LOCAL void create_step_table(oversample, output_fr)
  171. int oversample;     /* we sample oversample i for each byte output */
  172. int output_fr;      /* output frequency */
  173.    {
  174.    double note_fr; /* note frequency (in Hz) */
  175.    double step;
  176.    int pitch;      /* amiga pitch */
  177.  
  178.         /* special case: oversample of 0 means linear resampling */
  179.     if (oversample == 0)
  180.         oversample = 1;
  181.    step_table[0] = 0;
  182.    for (pitch = 1; pitch < REAL_MAX_PITCH + VIB_MAXDEPTH; pitch++)
  183.       {
  184.       note_fr = AMIGA_CLOCKFREQ / pitch;
  185.          /* int_to_fix(1) is the normalizing factor */
  186.       step = note_fr / output_fr * int_to_fix(1) / oversample;
  187.       step_table[pitch] = (long)step;
  188.       }
  189.    }
  190.          
  191. LOCAL void readjust_pitch()
  192.    {
  193.    int i;
  194.    for (i = 0; i < allocated; i++)
  195.       chan[i].step = step_table[chan[i].pitch];
  196.    }
  197.  
  198. void init_tables(oversample, frequency)
  199. int oversample, frequency;
  200.    {
  201.    create_step_table(oversample, frequency);
  202.    readjust_pitch();
  203.    }
  204.  
  205.  
  206. /* The playing mechanism itself.
  207.  * According to the current channel automaton,
  208.  * we resample the instruments in real time to
  209.  * generate output.
  210.  */
  211. void resample(oversample, number)
  212. int oversample;
  213. int number;
  214.    {
  215.    int i;            /* sample counter */
  216.    int channel;      /* channel counter */
  217.    int sampling;     /* oversample counter */
  218.     int step;         /* fractional part for linear resampling */
  219.    long value[NUMBER_SIDES];
  220.                      /* recombinations of the various data */
  221.    struct audio_channel *ch;
  222.  
  223.       /* safety check: we can't have a segv there, provided
  224.        * chan points to a valid sample.
  225.        * For `empty' samples, what is needed is fix_length = 0
  226.        * and rp_start = NULL
  227.        */
  228.  
  229.         /* do the resampling, i.e., actually play sounds */
  230.         /* code unfolding for special cases */
  231.     switch(oversample)
  232.         {
  233.     case 0:    /* linear resampling */
  234.       for (i = 0; i < number; i++) 
  235.          {
  236.             value[LEFT_SIDE] = value[RIGHT_SIDE] = 0;
  237.          for (channel = 0; channel < allocated; channel++)
  238.             {
  239.             ch = chan + channel;
  240.             switch(ch->mode)
  241.                {
  242.             case DO_NOTHING:
  243.                break;
  244.             case PLAY:
  245.                   /* Since we now have fix_length, we can
  246.                    * do that check with improved performance
  247.                    */
  248.                if (ch->pointer < ch->samp->fix_length)
  249.                   {
  250.                         step = fractional_part(ch->pointer);
  251.                         value[ch->side] += 
  252.                             ( (ch->samp->start[C] * (total_step - step) +
  253.                                ch->samp->start[C+1] * step)
  254.                              * (ch->scaled_volume>>8)
  255.                             ) >> (ACCURACY-8);
  256.                   ch->pointer += ch->step;
  257.                   break;
  258.                   }
  259.                else
  260.                   {
  261.                   ch->mode = REPLAY;
  262.                   ch->pointer -= ch->samp->fix_length;
  263.                   /* FALLTHRU */
  264.                   }
  265.             case REPLAY:
  266.                      /* is there a replay ? */
  267.                if (!ch->samp->rp_start)
  268.                   {
  269.                   ch->mode = DO_NOTHING;
  270.                   break;
  271.                   }
  272.                while (ch->pointer >= ch->samp->fix_rp_length)
  273.                   ch->pointer -= ch->samp->fix_rp_length;
  274.                     step = fractional_part(ch->pointer);
  275.                     value[ch->side] += 
  276.                          ( (ch->samp->rp_start[C] * (total_step - step) +
  277.                             ch->samp->rp_start[C+1] * step)>>8
  278.                          * (ch->scaled_volume >>8)
  279.                         ) >> (ACCURACY-8);
  280.                ch->pointer += ch->step;
  281.                break;
  282.                }
  283.             } 
  284.                 /* some assembly required... */
  285.          output_samples(value[LEFT_SIDE], value[RIGHT_SIDE]);
  286.          }
  287.         break;
  288.  
  289.     case 1:        /* no oversampling at all */
  290.       for (i = 0; i < number; i++) 
  291.          {
  292.             value[LEFT_SIDE] = value[RIGHT_SIDE] = 0;
  293.          for (channel = 0; channel < allocated; channel++)
  294.             {
  295.             ch = chan + channel;
  296.             switch(ch->mode)
  297.                {
  298.             case DO_NOTHING:
  299.                break;
  300.             case PLAY:
  301.                   /* Since we now have fix_length, we can
  302.                    * do that check with improved performance
  303.                    */
  304.                if (ch->pointer < ch->samp->fix_length)
  305.                   {
  306.                   value[ch->side] += ch->samp->start[C] * ch->scaled_volume; 
  307.                   ch->pointer += ch->step;
  308.                   break;
  309.                   }
  310.                else
  311.                   {
  312.                   ch->mode = REPLAY;
  313.                   ch->pointer -= ch->samp->fix_length;
  314.                   /* FALLTHRU */
  315.                   }
  316.             case REPLAY:
  317.                      /* is there a replay ? */
  318.                if (!ch->samp->rp_start)
  319.                   {
  320.                   ch->mode = DO_NOTHING;
  321.                   break;
  322.                   }
  323.                while (ch->pointer >= ch->samp->fix_rp_length)
  324.                   ch->pointer -= ch->samp->fix_rp_length;
  325.                value[ch->side] += ch->samp->rp_start[C] * ch->scaled_volume; 
  326.                ch->pointer += ch->step;
  327.                break;
  328.                     }
  329.             } 
  330.          output_samples(value[LEFT_SIDE], value[RIGHT_SIDE]);
  331.          }
  332.         break;
  333.     default:        /* standard oversampling code */
  334.       for (i = 0; i < number; i++) 
  335.          {
  336.             value[LEFT_SIDE] = value[RIGHT_SIDE] = 0;
  337.          for (channel = 0; channel < allocated; channel++)
  338.             {
  339.             for (sampling = 0; sampling < oversample; sampling++)
  340.                {
  341.                ch = chan + channel;
  342.                switch(ch->mode)
  343.                   {
  344.                case DO_NOTHING:
  345.                   break;
  346.                case PLAY:
  347.                      /* Since we now have fix_length, we can
  348.                       * do that check with improved performance
  349.                       */
  350.                   if (ch->pointer < ch->samp->fix_length)
  351.                      {
  352.                      value[ch->side] += ch->samp->start[C] * ch->scaled_volume;
  353.                      ch->pointer += ch->step;
  354.                      break;
  355.                      }
  356.                   else
  357.                      {
  358.                      ch->mode = REPLAY;
  359.                      ch->pointer -= ch->samp->fix_length;
  360.                      /* FALLTHRU */
  361.                      }
  362.                case REPLAY:
  363.                         /* is there a replay ? */
  364.                   if (!ch->samp->rp_start)
  365.                      {
  366.                      ch->mode = DO_NOTHING;
  367.                      break;
  368.                      }
  369.                   while (ch->pointer >= ch->samp->fix_rp_length)
  370.                      ch->pointer -= ch->samp->fix_rp_length;
  371.                   value[ch->side] += ch->samp->rp_start[C] * ch->scaled_volume;
  372.                   ch->pointer += ch->step;
  373.                   break;
  374.                   }
  375.                } 
  376.             }
  377.          output_samples(value[LEFT_SIDE]/oversample, 
  378.             value[RIGHT_SIDE]/oversample);
  379.          }   
  380.       }
  381.  
  382. #ifndef __NeXT__
  383.       /* On the NeXT you may not flush your buffer that often, because the
  384.        * collected data is not enough for continous play.
  385.        */
  386.    flush_buffer();
  387. #endif
  388.    }
  389.  
  390.  
  391. /* setting up a given note */
  392.  
  393. void play_note(au, samp, pitch)
  394. struct audio_channel *au;
  395. struct sample_info *samp;
  396. int pitch;
  397.    {
  398.    au->pointer = 0;
  399.    au->pitch = pitch;
  400.    au->step = step_table[pitch];
  401.     au->samp = samp;
  402.     au->scaled_volume = au->samp->volume_lookup[au->volume];
  403.     au->mode = PLAY;
  404.    }
  405.  
  406. /* changing the current pitch (value
  407.  * may be temporary, and not stored
  408.  * in channel pitch, for instance vibratos.
  409.  */
  410. void set_play_pitch(au, pitch)
  411. struct audio_channel *au;
  412. int pitch;
  413.    {
  414.       /* save current pitch in case we want to change
  415.        * the step table on the run
  416.        */
  417.    au->pitch = pitch;
  418.    au->step = step_table[pitch];
  419.    }
  420.  
  421. /* changing the current volume. You HAVE to get through
  422.  * there so that it will work on EVERY machine.
  423.  */
  424. void set_play_volume(au, volume)
  425. struct audio_channel *au;
  426. int volume;
  427.    {
  428.    au->volume = volume;
  429.     au->scaled_volume = au->samp->volume_lookup[volume];
  430.    }
  431.  
  432. void set_play_position(au, pos)
  433. struct audio_channel *au;
  434. int pos;
  435.    {
  436.    au->pointer = int_to_fix(pos);
  437.    }
  438.  
  439.